﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using D3D = SlimDX.Direct3D10;

namespace Tesla.Direct3D10.Graphics.Implementation {
    /// <summary>
    /// Concrete Direct3D10 implementation for <see cref="RasterizerState"/>.
    /// </summary>
    public sealed class D3D10RasterizerStateImplementation : RasterizerStateImplementation {
        private D3D10Renderer _renderer;
        private D3D.Device _graphicsDevice;
        private D3D.RasterizerState _rs;

        /// <summary>
        /// Gets the D3D10Renderer that created this implementation.
        /// </summary>
        internal D3D10Renderer Renderer {
            get {
                return _renderer;
            }
        }

        /// <summary>
        /// Gets the D3D10 RasterizerState.
        /// </summary>
        internal D3D.RasterizerState D3D10RasterizerState {
            get {
                return _rs;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="D3D10RasterizerStateImplementation"/>.
        /// </summary>
        /// <param name="renderer">The renderer.</param>
        internal D3D10RasterizerStateImplementation(D3D10Renderer renderer) 
            : base() {
            _renderer = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;
        }

        /// <summary>
        /// Binds the implementation. This is called the first time an unbound state is set to the device or manually by the user in order to
        /// create the underlying state ahead of time (best practice). Once called the state properties are read-only.
        /// </summary>
        public override void BindRasterizerState() {
            if(!base.IsBound) {
                D3D.RasterizerStateDescription desc = new D3D.RasterizerStateDescription();
                desc.CullMode = D3D10Helper.ToD3DCullMode(base.Cull);
                desc.FillMode = D3D10Helper.ToD3DFillMode(base.Fill);
                desc.IsMultisampleEnabled = base.EnableMultiSampleAntiAlias;
                desc.SlopeScaledDepthBias = base.SlopeScaledDepthBias;
                desc.DepthBias = base.DepthBias;
                desc.DepthBiasClamp = 100.0f; //TODO: Whats a good number here?
                desc.IsFrontCounterclockwise = (base.VertexWinding == VertexWinding.CounterClockwise) ? true : false;
                desc.IsScissorEnabled = base.EnableScissorTest;
                desc.IsAntialiasedLineEnabled = base.EnableMultiSampleAntiAlias;

                _rs = D3D.RasterizerState.FromDescription(_graphicsDevice, desc);
                base.IsBound = true;

                //Add to tracker
                _renderer.Resources.AddTrackedObject(_rs.ComPointer, this);
            }
        }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="D3D10RasterizerStateImplementation"/> is reclaimed by garbage collection.
        /// </summary>
        ~D3D10RasterizerStateImplementation() {
            Dispose(false);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing) {
            if(!IsDisposed) {
                //Dispose of managed resources
                if(disposing) {
                    if(_rs != null) {
                        if(_renderer != null) {
                            _renderer.Resources.RemoveTrackedObject(_rs.ComPointer);
                        }
                        _rs.Dispose();
                        _rs = null;
                    }
                }
                base.Dispose(disposing);
            }
        }
    }
}
